package com.common.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.common.model.Biotech;
import com.common.model.User;
import com.common.service.BiotechService;
import com.common.service.UserService;
import com.common.util.CreateId;
import com.common.util.Utils;



public class BiotechAction extends BaseAction{
	private BiotechService bioservice;
	private UserService userservice;
	private Biotech biotech;
	private String id;
	private List<Biotech> list = new ArrayList<Biotech>();
	private List<User> weixiulist = new ArrayList<User>();
	private String keyword;
	private String author;
	private String repairman;
	private String username;
	private int status;
	private String zuobiao;
	private String pingjia;
	private String jsonString;
	
	
	public String list(){
		if(biotech == null){
			if(repairman == null){
				list = bioservice.search(null);
			}else{
				//查找维修人员相应的维修记录
				list = bioservice.listrepair_(repairman);
			}
		}else{
			list = bioservice.search(biotech);
		}
		return SUCCESS;
	}
	
	
	
	
	public String map(){
		return SUCCESS;
	}
	
	public String delete(){
		bioservice.del(biotech);
		return SUCCESS;
	}
	
	public String paiin(){
		weixiulist = userservice.list_weixiu();
		biotech = bioservice.load(biotech.getId());
		return SUCCESS;
	}
	
	public String pai(){
		bioservice.pai(biotech);
		return SUCCESS;
	}
	
	//查看报修记录
	public String listAuthor(){
		if(biotech == null){
			if(author == null){
				list = bioservice.search(null);
			}else{
				list = bioservice.loadAllJson0_(author);
			}
			
		}else{
			list = bioservice.search(biotech);
		}
		
		return SUCCESS;
	}
	
	//添加报修
	public String save(){
		String realpath = ServletActionContext.getServletContext().getRealPath(
		"/upload");

		File file_= new File(realpath);
		if (!file_.exists()) {
			file_.mkdirs();
		}
		String  filename = null;
		if(biotech.getFileFileName()!=null){
			filename = biotech.getFileFileName();
		}

		String extention = Utils.getExtensionName(filename);
		filename = CreateId.getId()+"."+extention;
		try {
			if(biotech.getFile()!=null){
				FileUtils.copyFile(biotech.getFile(), new File(file_, filename));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(filename!= null){
			biotech.setImage_url("upload" + "\\" + filename);
		}
		
		if(biotech.getPingjia()==null){
			biotech.setStatus(0);
		}
		bioservice.save(biotech);
		return SUCCESS;
	}
	
	//查看未接单的维修任务
	public String listRepair0(){
		if(biotech == null){
			if(username == null){
				list = bioservice.search(null);
			}else{
				Biotech biotech = new Biotech();
				biotech.setStatus(0);
				list = bioservice.listrepair0(biotech);
			}
		}else{
			list = bioservice.search(biotech);
		}
		return SUCCESS;
	}
	
	public String jiedan(){
		boolean flag =bioservice.updatestatus(2, biotech);
		if(flag){
			jsonString="1";
		}else{
			jsonString="0";
		}
		
		return SUCCESS;
	}
	
	//查看维修记录
	public String listRepair1(){
		if(biotech == null){
			if(username == null){
				list = bioservice.search(null);
			}else{
				list = bioservice.listrepair1(username);
			}
			
		}else{
			Biotech biotech = new Biotech();
			biotech.setRepairman(username);
			list = bioservice.search(biotech);
		}
		
		return SUCCESS;
	}
	
	   //添加评价
		public String pingjiaDetail(){
			bioservice.save(biotech);
			
			
			return "保存成功";
		}
	
	//查看维修记录
		public String pingjia(){
				if(username == null){
					list = bioservice.search(null);
			}else{
				list = bioservice.yixiu(username);
			}
			
			return SUCCESS;
		}
	public String complete(){
		boolean flag =bioservice.updatestatus(3, biotech);
		if(flag){
			jsonString="1";
		}else{
			jsonString="0";
		}
		return SUCCESS;
	}
	
	
	
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public UserService getUserservice() {
		return userservice;
	}
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Biotech> getList() {
		return list;
	}

	public void setList(List<Biotech> list) {
		this.list = list;
	}

	public BiotechService getBioservice() {
		return bioservice;
	}

	public void setBioservice(BiotechService bioservice) {
		this.bioservice = bioservice;
	}

	public Biotech getBiotech() {
		return biotech;
	}
	public void setBiotech(Biotech biotech) {
		this.biotech = biotech;
	}
	public String getRepairman() {
		return repairman;
	}
	public void setRepairman(String repairman) {
		this.repairman = repairman;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public List<User> getWeixiulist() {
		return weixiulist;
	}
	public void setWeixiulist(List<User> weixiulist) {
		this.weixiulist = weixiulist;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getZuobiao() {
		return zuobiao;
	}
	public void setZuobiao(String zuobiao) {
		this.zuobiao = zuobiao;
	}
	
	
	
	public String updatestatus(){
		
		boolean flag =bioservice.updatestatus(status, biotech);
		if(flag){
			jsonString="1";
		}else{
			jsonString="0";
		}
		
		return SUCCESS;
	}
	
	public String uploadarticle(){
		String realpath = ServletActionContext.getServletContext().getRealPath(
		"/upload");
		
		File file_= new File(realpath);
		if (!file_.exists()) {
			file_.mkdirs();
		}
		String filename = biotech.getFileFileName();
		String extention = Utils.getExtensionName(filename);
		filename = CreateId.getId()+"."+extention;
		
		try {
			FileUtils.copyFile(biotech.getFile(), new File(file_, filename));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			biotech.setPhone(URLDecoder.decode(biotech.getPhone(), "utf-8"));
			biotech.setAddress(URLDecoder.decode(biotech.getAddress(), "utf-8"));
			biotech.setNeedtime(URLDecoder.decode(biotech.getNeedtime(), "utf-8"));
//			biotech.setAuthor(URLDecoder.decode(biotech.getAuthor(), "utf-8"));
			biotech.setContent(URLDecoder.decode(biotech.getContent(), "utf-8"));
			biotech.setZuobiao(URLDecoder.decode(biotech.getZuobiao(), "utf-8"));
			biotech.setTitle(URLDecoder.decode(biotech.getTitle(), "utf-8"));
			biotech.setType(URLDecoder.decode(biotech.getType(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		biotech.setImage_url("upload" + "\\" + filename);
		biotech.setStatus(0);
		boolean flag = bioservice.save(biotech);
		if(flag){
			jsonString ="1";
		}else{
			jsonString ="0";
		}
		return SUCCESS;
	}
	
	public String listjson(){
			
		if(keyword !=null&& !keyword.equals("")){
			jsonString=bioservice.loadAllJson2(biotech);
		}else{
			jsonString=bioservice.loadAllJson();
		}
		return SUCCESS;
	}
	
	public String listjson0(){
		jsonString=bioservice.loadAllJson0(author);
		return SUCCESS;
	}
	
	public String listjson1(){
		jsonString=bioservice.listrepair(repairman);
		return SUCCESS;
	}
	public String listjson3(){
		jsonString=bioservice.list3();
		return SUCCESS;
	}
	
	public String detailjson(){
		jsonString=bioservice.detailjson(id);
	    return SUCCESS;
	}

	public String getPingjia() {
		return pingjia;
	}

	public void setPingjia(String pingjia) {
		this.pingjia = pingjia;
	}
	
}
